

#include "../include/tricks.h"


int counting_sort(int *src, int *dest, int len, int max)
{
    int *counter = (int *)malloc(sizeof(int) * (max + 1));
    if (!counter)
        return -1;

    memset(counter, 0, sizeof(int) * len);
    memset(dest, 0, sizeof(int) * len);

    int i,j,p = 0;
    for (i = 0; i < len; i++)
    {
        counter[src[i]] += 1;
    }

    for (i = 0; i < max + 1; i++)
    {
        for (j = 0; j < counter[i]; j++)
        {
            dest[p] = i;
            p++;
        }
    }

    return 0;
}
